<template>
  <div class="login-box">
    <div class="login-logo-box">
      <img src="../assets/img/logo.svg" alt="">
    </div>
    <el-form :model="ruleForm2" :rules="rules2" ref="ruleForm2" label-position="left" label-width="0px" class="demo-ruleForm login-container">
      <h3 class="title">管理员登录</h3>
      <el-form-item prop="account">
        <el-input type="text" class="login-input" v-model="ruleForm2.account" auto-complete="off" placeholder="账号"></el-input>
      </el-form-item>
      <el-form-item prop="checkPass">
        <el-input type="password" class="login-input" v-model="ruleForm2.checkPass" auto-complete="off" placeholder="密码"></el-input>
      </el-form-item>
      <el-checkbox v-model="checked" class="remember">记住密码</el-checkbox>
      <el-form-item style="width:100%;">
        <el-button type="primary" style="width:100%;" @click.native.prevent="handleSubmit2" :loading="logining">登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
  import { requestLogin } from '../api/api';
  export default {
    data() {
      return {
        logining: false,
        ruleForm2: {
          account: '',
          checkPass: ''
        },
        rules2: {
          account: [
            { required: true, message: '请输入账号', trigger: 'blur' },
          ],
          checkPass: [
            { required: true, message: '请输入密码', trigger: 'blur' },
          ]
        },
        checked: true
      };
    },
    methods: {
      handleReset2() {
        this.$refs.ruleForm2.resetFields();
      },
      handleSubmit2(ev) {
        var _this = this;
        this.$refs.ruleForm2.validate((valid) => {
          if (valid) {
            this.logining = true;
            let loginParams = {
              schoolNumber: '',
              pwd: '',
              roleType: 2
            };
            loginParams.schoolNumber = this.ruleForm2.account;
            loginParams.pwd = this.ruleForm2.checkPass;
            requestLogin(loginParams).then(res => {
              this.logining = false;
              let { data, msg, status } = res;
              if (msg !== 'OK' || status !== 200) {
                this.$message({
                  message: msg,
                  type: 'error'
                });
              } else {
                if(this.checked){
                  let storage = window.localStorage;
                  storage['schoolNumber'] = loginParams.schoolNumber;
                  storage['pwd'] = loginParams.pwd;
                }
                sessionStorage.setItem('user', JSON.stringify(data));
                this.$router.push({ path: '/main' });
              }
            });
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      }
    },
    mounted: function () {
      let storage = window.localStorage;
      if(storage['schoolNumber']!==''){
        this.ruleForm2.account = storage['schoolNumber'];
        this.ruleForm2.checkPass = storage['pwd'];
        this.checked = true
      }
    }
  }

</script>

<style lang="less">
  @import '../styles/lib.less';
  .login-box{
    .boxSize1(100%);
    padding-top: 150px;
    .BC(@black-4);
    .login-logo-box{
      text-align: center;
      img{
        .boxSizeH(30px);
        margin-bottom: 50px;
      }
    }
  }
  .login-container {
    margin: 0 auto;
    .boxSizeW(400px);
    padding: 10px 30px 30px;
    .BC(@white-1);
    .title {
      margin: 15px auto 35px auto;
      text-align: center;
      color: @black-1;
      font-family: '幼圆','Microsoft YaHei',sans-serif;
      letter-spacing: 2px;
      .font2(28px,normal);
    }
    .login-input{
      input{
        border: 0;
        background-color: @light-gray-2;
        .borderRadius(0)
      }
    }
    .remember {
      margin: 0 0 35px 3px;
    }
  }
</style>